package com.cskaoyan.mall.controller;

import com.cskaoyan.mall.model.Goods;
import com.cskaoyan.mall.model.GoodsParam;
import com.cskaoyan.mall.model.Result;
import com.cskaoyan.mall.model.Type;
import com.cskaoyan.mall.service.GoodsService;
import com.cskaoyan.mall.service.GoodsServiceImpl;
import com.cskaoyan.mall.utils.FileUploadUtils;
import com.cskaoyan.mall.utils.HttpUtils;
import com.google.gson.Gson;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @PackgeName: ${PACKAGE_NAME}
 * @ClassName: ${NAME}
 * @Author: admin
 * Date: 2020/5/3 20:19
 * project name: Javaproject1
 * @Version:
 * @Description:
 */
@WebServlet("/api/admin/goods/*")
public class GoodsServlet extends HttpServlet {

    private GoodsService goodsService = new GoodsServiceImpl();

    Gson gson = new Gson();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String requestURI = request.getRequestURI();
        String action = requestURI.replace("/api/admin/goods/", "");
        if ("imgUpload".equals(action)) {
            imgUpload(request, response);
        } else if ("addGoods".equals(action)) {
            addGoods(request, response);
        }
    }


    /**
     * 添加商品
     * @param request
     * @param response
     */
    private void addGoods(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String requestBody = HttpUtils.getRequestBody(request);
        GoodsParam goodsParam = gson.fromJson(requestBody, GoodsParam.class);
        goodsService.addGoods(goodsParam);
        Result result = new Result();
        result.setCode(0);
        response.getWriter().println(gson.toJson(result));
    }

    /**
     * 单独的文件上传，不会含有普通的form表单数据
     * 传输的文件二进制文件就在请求体内
     * 但是里面有分隔符WebKitFormBoundary
     *
     * @param request
     * @param response
     */
    private void imgUpload(HttpServletRequest request, HttpServletResponse response) throws IOException {

        //为了稳妥起见，可以先校验一下是否含有multipartContent
        boolean content = ServletFileUpload.isMultipartContent(request);
        if (!content) {
            //return 10000 message
        }

        Map<String, Object> map = FileUploadUtils.parseRequest(request);
        System.out.println(map);

        //细节点需要了解
        //8080端口下取图片只会在8080端口下进行，但是8080端口没有图片，
        //如果要完成取图的功能，需要在8084端口下。
        String domain = (String) getServletContext().getAttribute("domain");

        String file = (String) map.get("file");
        file = domain + file;
        System.out.println(file);

        Result result = new Result();
        result.setCode(0);
        result.setData(file);
        response.getWriter().println(gson.toJson(result));
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String requestURI = request.getRequestURI();
        String action = requestURI.replace("/api/admin/goods/", "");

        if ("getType".equals(action)) {
            getType(request, response);
        } else if ("getGoodsByType".equals(action)) {
            getGoodsByType(request, response);
        }
    }

    /**
     * 根据相应的分类查询当前分类下的商品
     * @param request
     * @param response
     */
    private void getGoodsByType(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String typeId = request.getParameter("typeId");
        //先来一些校验？
        List<Goods> goods = goodsService.getGoodsByType(typeId);
        Result result = new Result();
        result.setCode(0);
        result.setData(goods);
        response.getWriter().println(gson.toJson(result));
    }

    /**
     * 查询商品前的查询分类
     * 1.抓包
     * 2.分析逻辑
     * 3.
     * @param request
     * @param response
     */
    private void getType(HttpServletRequest request, HttpServletResponse response) throws IOException {
        List<Type> typeList = goodsService.getType();
        Result result = new Result();
        result.setCode(0);
        result.setData(typeList);
        response.getWriter().println(gson.toJson(result));
    }
}
